package com.kookoo.utils
{
	import mx.core.UIComponent;
	
	import flash.display.Graphics;
	import flash.display.Sprite;

	import mx.containers.ViewStack;
	import mx.containers.Canvas;
	import flash.events.MouseEvent;
	import mx.controls.Image;
	import mx.styles.StyleManager;
	import mx.styles.CSSStyleDeclaration;
	import flash.events.Event;

	[Style(name="backgroundColor", type="uint", format="Color", inherit="no")]    	
	[Style(name="borderColor", type="uint", format="Color", inherit="no")]
	[Style(name="cornerRadius", type="uint", format="Number", inherit="no")]    	
	[Style(name="borderThickness", type="uint", format="Number", inherit="no")]
	[Event(name="click", type="flash.events.MouseEvent")]
	
	public class ThumbBrowser extends UIComponent
	{
		public function ThumbBrowser()	{ super(); }
		
		private var _previewImage:ImageProxy;
		private var _dataProvider:Object;
		
		public function get dataProvider():Object {
			return _dataProvider;
		}
		
		public function set dataProvider(value:Object) : void {
			_dataProvider = value;
			invalidateProperties();
		}
		
		private var _selectedIndex: uint;
		
		public function get selectedIndex():uint {
			return _selectedIndex;
		}
		
		public function set selectedIndex(value:uint) : void {
			_selectedIndex = value;
		}		

		private var _selectedItem: String;
		
		public function get selectedItem():String {
			return _selectedItem;
		}
		
		public function set selectedItem(value:String) : void {
			_selectedItem = value;
		}
		
		protected override function createChildren():void {
			super.createChildren();
			this.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			this.addEventListener(MouseEvent.MOUSE_OVER, function (): void {_previewImage.visible = false})
			this.addEventListener(MouseEvent.MOUSE_OUT, function (): void {_previewImage.visible = true})
			this.addEventListener(MouseEvent.CLICK, clickHandler, true);
		}
		
		private function moveHandler(e:MouseEvent) : void {
			var visibleImage: uint = Math.round((e.localX/this.width) * (_dataProvider.length-1));
			for(var a:uint = 0; a<_dataProvider.length; a++) {
				this.getChildAt(a).visible = false;
			}
			
			(this.getChildAt(visibleImage) as ImageProxy).visible = true;
		}
		
		private function clickHandler(e:MouseEvent): void {
			var visibleImage: uint = Math.round((e.localX/this.width) * (_dataProvider.length-1));
			_selectedIndex = visibleImage;
			_selectedItem = (this.getChildAt(visibleImage) as ImageProxy).filename;
			trace(_selectedItem)
			//dispatchEvent(new Event(MouseEvent.CLICK));
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			var g:Graphics = this.graphics;
			g.beginFill(getStyle("backgroundColor"));
			g.drawRect(0, 0, this.width, this.height);
			g.endFill();
			
			
			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0x000000);
			mask.graphics.drawRoundRect(0,0, this.width, this.height, getStyle("cornerRadius"), getStyle("cornerRadius"))
			mask.graphics.endFill();
			addChild(mask);
			this.mask = mask;
			
		}
		
		protected override function commitProperties():void {
			super.commitProperties();
			
			
			for (var a:uint = 0; a<_dataProvider.length; a++) 
			{
				var i:ImageProxy = new ImageProxy();
				i.width = this.width
				i.height = this.height;
				i.visible = false;
				i.filename = _dataProvider[a];
				i.load();
				addChild(i);
				
			}
			
			_previewImage = new ImageProxy();
			_previewImage.scaleMode = "fill";
			_previewImage.width = this.width
			_previewImage.height = this.height;
			_previewImage.filename = _dataProvider[0]
			_previewImage.load();
			addChild(_previewImage);
			
			// draw border
			if (getStyle("borderThickness") > 0) {
				var s:Sprite = new Sprite();
				s.graphics.beginFill(0,0);
				s.graphics.lineStyle(getStyle("borderThickness") * 2, getStyle("borderColor"));
				s.graphics.drawRoundRect(0,0, this.width, this.height, getStyle("cornerRadius"), getStyle("cornerRadius"))
				s.graphics.endFill();
				addChild(s);
			}
			
		}
		
		protected override function measure():void {
			super.measure();
		}
		
		// Define a static variable.
        private static var classConstructed:Boolean = classConstruct();
        
        // Define a static method.
        private static function classConstruct():Boolean {
            if (!StyleManager.getStyleDeclaration("ThumbBrowser"))
            {
                // If there is no CSS definition for TagFilter,  then create one and set the default value.
                var newStyleDeclaration:CSSStyleDeclaration = new CSSStyleDeclaration();
                newStyleDeclaration.setStyle("backgroundColor", 0x999999);
                StyleManager.setStyleDeclaration("ThumbBrowser", newStyleDeclaration, true);
               
                newStyleDeclaration.setStyle("borderColor", 0xffffff);
                StyleManager.setStyleDeclaration("ThumbBrowser", newStyleDeclaration, true);
               
                newStyleDeclaration.setStyle("cornerRadius", 15);
                StyleManager.setStyleDeclaration("ThumbBrowser", newStyleDeclaration, true);
               
                newStyleDeclaration.setStyle("borderThickness", 0);
                StyleManager.setStyleDeclaration("ThumbBrowser", newStyleDeclaration, true);
            }
            return true;
        }
		
	}
}